package com.growthgate.bootcommon.common.base;

import com.growthgate.bootcommon.annotation.ApiIgnoreFields;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * BaseController
 *
 * @author GrowthGate
 * @date 2020/2/17
 */
public class BaseEntityController<E extends BaseEntity, S extends BaseService<E>> extends BaseController {

    protected S service;

    public BaseEntityController(S service) {
        this.service = service;
    }

    /**
     * 保存数据，方法名如果为“save”会有API文档报错的不明bug
     *
     * @param entity 实体对象
     */
    @PutMapping
    @ApiOperation("保存")
    public void entity(@ApiParam("实体对象") @ApiIgnoreFields({"createTime", "updateTime"}) @RequestBody E entity) {
        service.save(entity);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public void deleteById(@ApiParam("目标id") @PathVariable long id) {
        service.deleteById(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取")
    public E findById(@ApiParam("目标id") @PathVariable long id) {
        return service.findById(id);
    }

    @PostMapping
    @ApiOperation("分页查询")
    public Page<E> findAll(@ApiParam("查询条件") @RequestBody E entity,
            @ApiParam("页数") @RequestParam(defaultValue = "0") int pageNum,
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int pageSize) {
        return service.page(entity, pageNum, pageSize);
    }

}